package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.OutStock;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoHong
 * @description 发放记录接口
 * @date 2022/2/18
 */
@RestController
@Api(tags = " 发放记录接口")
@RequestMapping("/business/outStock")
public class OutStockController {
    @Autowired
    private OutStockService outStockService;
    //http://www.localhost:8989/business/outStock/findOutStockList?pageNum=1&pageSize=10&status=0&outNum=123456677&type=0
    //发放记录列表
    @GetMapping("/findOutStockList")
    public ResponseBean<PageVO<OutStockVO>> findOutStockList(@RequestParam("pageNum") Integer pageNum,
                                                           @RequestParam("pageSize") Integer pageSize,
                                                           OutStockVO outStockVO){  //实际上传过来的几个过滤条件用实体类接受了；
        PageVO<OutStockVO>  outStockVOPageVO= outStockService.findOutStockList(outStockVO,pageNum,pageSize);
        return ResponseBean.success(outStockVOPageVO);
    }


    //获取出库记录明细
//    http://www.localhost:8989/business/outStock/detail/14?pageNum=1
    @GetMapping("/detail/{id}")
    public ResponseBean<OutStockDetailVO> detail(
            @RequestParam("pageNum") Integer pageNum,
            @PathVariable Long id //这个是outStock的主键
    ){
        OutStockDetailVO outStockDetailVO =outStockService.detail(id,pageNum);
        List<OutStockItemVO> page = ListPageUtils.page(outStockDetailVO.getItemVOS(), 3, pageNum);
        outStockDetailVO.setTotal(outStockDetailVO.getItemVOS().size());
        outStockDetailVO.setItemVOS(page);
        return ResponseBean.success(outStockDetailVO);
    }

//    http://www.localhost:8989/business/outStock/addOutStock
    //添加新的出库信息
    @PostMapping("/addOutStock")
    public ResponseBean addOutStock(@RequestBody OutStockVO outStockVo,
                                    @RequestHeader(value = "Authorization",required = false)String token) {
        String username = JWTUtils.getUsername(token);
        outStockService.addOutStock(outStockVo,username);
        return ResponseBean.success();
    }

    //将出库单删除
    @GetMapping("/delete/{outStockId}")
    public ResponseBean delete(@PathVariable Long outStockId) {
        outStockService.delete(outStockId);
        return ResponseBean.success();
    }

    //将出库信心移入到回收站
    @PutMapping("/remove/{outStockId}")
    public ResponseBean remove(@PathVariable Long outStockId) {
        outStockService.remove(outStockId);
        return ResponseBean.success();
    }

    //将入库信心从回收站恢复
    @PutMapping("/back/{outStockId}")
    public ResponseBean back(@PathVariable Long outStockId) {
        outStockService.back(outStockId);
        return ResponseBean.success();
    }


    //将出库物资通过审核
    @PutMapping("/publish/{outStockId}")
    public ResponseBean publish(@PathVariable Long outStockId) {
        outStockService.publish(outStockId);
        return ResponseBean.success();
    }
}
